<?php

namespace app\admin\model;

use app\admin\controller\Qr;
use think\Db;
use think\Model;
use think\Validate;

class Admin extends Model
{
    //用户登录
    public function login($data){
        $userInfo=$this->where('admin_username',$data['username'])->where('admin_password',md5($data['password']))->find();
     // model('Admin')->getLastSql();
       if($userInfo['status']!=1){
        if(!$userInfo){
            //失败！！
            return ['code'=>1,'msg'=>'用户名或密码不正确'];
        }
        session('admin. Id',$userInfo['Id']);
        session('admin.admin_username',$userInfo['admin_username']);
        return ['code'=>0,'msg'=>'登录成功','data'=>$data,'count'=>"20"];
       }else{
           return ['code'=>1,'msg'=>'账号已被停用'];
       }
    }
    //修改密码
    public function pass($data){
        $validate = new Validate([
            'password'  => 'require',
            'new_password'  => 'require',
            'confirm_password'  => 'require|confirm:new_password',
        ],[
            'password.require'  => '请输入原始密码',
            'new_password.require'  => '请输入新密码',
            'confirm_password.require'  => '请输入确认密码',
            'confirm_password.confirm'  => '确认密码有误',
        ]);
        if (!$validate->check($data)) {
            return  ['valid'=>0,'msg'=>$validate->getError()];
        }
        $userInfo=$this->where('Id',session('admin.Id'))->where('admin_password',md5($data['password']))->find();
        if(!$userInfo){
            return ['valid'=>0,'msg'=>'原始密码不对！！！'];
        }
        $res=$this->save([
            'admin_password'=>md5($data['new_password']),
        ],[$this->pk => session('admin.Id')]);
        if ($res){
            return ['valid'=>1,'msg'=>'修改成功！！'];
        }else{
            return['valid'=>0,'msg'=>'修改失败！！'];
        }
    }
    //查询所有用户
    public function searchall(){
        $info=$this->select();
        if(!$info){
            //失败！！
            return ['code'=>1,'msg'=>'失败'];
        }
             return $info;
    }
    //新增用户
    public function addUser($data){
    $res=[
      'admin_username'=>$data['username'],
        'admin_password'=>md5($data['password']),
        'status'=>1
    ];
    $user=Db::name('admin')->insert($res);
    return ['code'=>0,'msg'=>'新增成功'];
    }
    //启用
    public function Upuser($data){
        $as=implode(",",$data);
        $arr=explode(',',$as);
        foreach ($arr as $key => $v1){
            $src=(new Qr())->create($v1);
            $res=Db::name('admin')->where('Id',$v1)->update([
                'status'=>0,
                'src'=>$src
            ]);

        }
    }
    //停用
    public function Douser($data){
        $as=implode(",",$data);
        $arr=explode(',',$as);
        foreach ($arr as $key => $v1){
            $src=(new Qr())->create($v1);
            $res=Db::name('admin')->where('Id',$v1)->update([
                'status'=>1,
                'src'=>$src
            ]);

        }
    }

    public function delUser($data){
        $data=$data['idsStr'];
        $dt=Db::name('admin')->delete($data);
        return ['code'=>0,'msg'=>'删除成功'];
    }




}
